package vinnichenko.lab3;

/**Own packages*/
import vinnichenko.lab3.database.*;
import vinnichenko.lab3.exception.*;

/**Java core packages*/
import java.io.*;
import java.util.*;

/**Java extension packages*/
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

/**Custom tag handler that reads information from the employees
 * database and makes that data available in a JSP.
 * @author Igor Vinnichenko
 * @version 1.0
 */
public class EmployeesListTag extends BodyTagSupport{
    /**UID*/
    private static final long serialVersionUID = 3L;

    /**Declaration and creation logger.*/
    static Logger logger = Logger.getLogger(EmployeesListTag.class);

    /**Hierarchy of employees representation*/
    private EmployeeDB db;
    /**Current employee's data*/
    private EmployeeInfo employee;
    /**Iterator*/
    private Iterator iterator;

    /**Method called to begin tag processing
     */
    public int doStartTag() throws JspException{
        /** attempt tag processing*/
        try{
            /**Create new database object and connect it to DAO token from application context.*/
            db = new EmployeeDB();
            EmployeesDAO empDAO = 
                (EmployeesDAO)pageContext.getAttribute("eDB", PageContext.APPLICATION_SCOPE);
            db.setDatabase( empDAO );
         
            /**Get data from database*/
            List<EmployeeInfo> list = db.getHierarchyList();
            iterator = list.iterator();

            if( iterator.hasNext() ){
                processNextEmployee();

                /**continue body processing*/ 
                return EVAL_BODY_AGAIN;
            }else 
                /**terminate body processing*/
                return SKIP_BODY;
        }catch( Exception exception ){
            /** if any exceptions occur, do not continue processing tag's body*/
            logger.error(StackWriter.getFullStackTrace( exception ));
            throw new JspException( exception );

            /**ignore the tag's body*/
            //return SKIP_BODY;
        }
    }

    /**Process body and determine if body processing should continue.
     *
     */
    public int doAfterBody(){
        /**attempt to output body data*/
        try {
            bodyContent.writeOut( getPreviousOut() );
        }catch ( IOException ioException ) {
            /**if exception occurs, terminate body processing*/
            logger.error(StackWriter.getFullStackTrace( ioException ));

            /**terminate body processing*/
            return SKIP_BODY;
        }

        bodyContent.clearBody();
      
        if ( iterator.hasNext() ) {
            processNextEmployee();

            /**continue body processing*/
            return EVAL_BODY_AGAIN;
        }else 
            /**terminate body processing*/
            return SKIP_BODY;
    }
   
    /**Obtain the next EmployeeBean and extract its data
     */
    private void processNextEmployee(){
        /**get next employee*/
        employee = ( EmployeeInfo ) iterator.next();

        /**Set attributes to page context*/
        pageContext.setAttribute( "empno",  employee.getEmployeeNo() );
        pageContext.setAttribute( "mgr",    employee.getManager() );
        pageContext.setAttribute( "deptno", employee.getDepartmentNo() );
        pageContext.setAttribute( "ename",  employee.getEmployeeName() );
        pageContext.setAttribute( "job",    employee.getJob() );
        pageContext.setAttribute( "sal",    employee.getSalary() );
        pageContext.setAttribute( "comm",   employee.getCommisions() );
        pageContext.setAttribute( "hiredate", employee.getHireDate() );
        pageContext.setAttribute( "level", employee.getLevel() );
    }
}
